import 'dart:convert';

import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateCreateDto.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateDto.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/src/components/cell/td_cell.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class OrganizationStatisticsTaskFillController extends GetxController {
  final OrganizationStatisticsTaskFillState state =
      OrganizationStatisticsTaskFillState();

  var id = Get.arguments;

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();

    state.taskInfo =
        await OrganizationRequest.getMyReceiveStatisticsTaskInfo(id);

    var data = json.decode(state.taskInfo.taskInfo!);
    state.formItem = data
        .map<OrganizationAnnouncementTemplateCreateDto>(
            (item) => OrganizationAnnouncementTemplateCreateDto.fromJson(item))
        .toList();
  }

  void showTimePicker(index) {
    var dto = state.formItem[index];

    var useYear = false;
    var useMonth = false;
    var useDay = false;
    var useHour = false;
    var useMinute = false;

    if(dto.attributes!.timeindex == 1){
      // 年月日时分
      useYear = true;
      useMonth = true;
      useDay = true;
      useHour = true;
      useMinute = true;
    }else if(dto.attributes!.timeindex == 2){
      // 年月日
      useYear = true;
      useMonth = true;
      useDay = true;
    }else if(dto.attributes!.timeindex == 3){
      // 时分
      useHour = true;
      useMinute = true;
    }
    TDPicker.showDatePicker(Get.context, title: '选择时间', onConfirm: (selected) {
      Get.back();
      var value = '${selected['year'].toString().padLeft(4, '0')}-${selected['month'].toString().padLeft(2, '0')}-${selected['day'].toString().padLeft(2, '0')} ${selected['hour'].toString().padLeft(2,'0')}:${selected['minute'].toString().padLeft(2,'0')}';

      if(dto.attributes!.timeindex == 2){
        value = '${selected['year'].toString().padLeft(4, '0')}-${selected['month'].toString().padLeft(2, '0')}-${selected['day'].toString().padLeft(2, '0')}';
      }
      if(dto.attributes!.timeindex == 3){
        value = '${selected['hour'].toString().padLeft(2,'0')}:${selected['minute'].toString().padLeft(2,'0')}';
      }
      state.formItem[index] = dto.copyWith(
          value:  value);
      update();
    },useYear: useYear,
       useMonth: useMonth,
       useDay: useDay,
       useHour: useHour,
       useMinute: useMinute,
   );
  }

  void showRadioPicker(index) {
    var dto = state.formItem[index];

    List<String> data = dto.attributes!.radioList!.map((e) => e.value!).toList();

    TDPicker.showMultiPicker(Get.context, title: '选择${dto.textInput}',
        onConfirm: (selected) {
          state.formItem[index] = dto.copyWith(
              value:  data[selected[0]]);
          update();
          Get.back();
        }, data: [data]);
  }

  void publish() async{

    await OrganizationRequest.submitStatisticsTask({
      "id":id,
      "taskInfo": json.encode(state.formItem)
    });

    SmartDialog.showNotify(msg: '提交成功', notifyType: NotifyType.success);
    Get.back();

  }

  void inputChange(index,val) {
    var dto = state.formItem[index];

    state.formItem[index] = dto.copyWith(
        value:  val);
    update();
  }
}
